<?php
/**
 * 托运单管理
 *
 *
 */
class shipping_list_model extends Model
{
    var $id;
	
	var $shipping_no;
	
	var $shipper;
	
	var $shipper_telephone;
	
	var $receiver;
	
	var $receiver_telephone;
	
	var $delivery_methmod;
	
	var $payment_mode;
	
	var $goods_name;
	
	var $goods_num;
	
	var $goods_packet_num;
	
	var $goods_value;
	
	var $transfer_station;	
		
	var $freight;
	
	var $cod_fee;
	
	var $actual_fee;
	
	var $transfer_fee;
	
	var $checkin_site;
	
	var $destination_site;	
	
	var $operator;
	
	var $operate_datetime;
		
    var $demo;

	function __construct()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

    /**
	 * load by id
	 *
	 *
	 */	
    function load($id)
    {
        if (!$id){
            return array();
        }

        $query = $this->db->get_where('shipping_list',array('id' => $id));

        if ($row = $query->row_array()){
 
            
            return $row;
        }

        return array();
    }
    /*
     * load by id from view
     */
    function loadv($id)
    {
    	if (!$id){
    		return array();
    	}
    
    	$query = $this->db->get_where('v_shipping_list',array('id' => $id));
    
    	if ($row = $query->row_array()){
    
    
    		return $row;
    	}
    
    	return array();
    }
    /**
     * load by id for shipping_list
     * 
     */
    
    function load_view($id)
    {
        if (!$id){
            return array();
        }

        $query = $this->db->get_where('v_shipping_list',array('id' => $id));

        if ($row = $query->row_array()){
 
            
            return $row;
        }

        return array();
    }  
    /*
     * 根据托运单编号查询托运单
     * 
     */
    function load_shipping($id)
    {
    	if (!$id){
    		return array();
    	}
    
    	$query = $this->db->get_where('v_shipping_list',array('shipping_no' => $id));
    
    	if ($row = $query->row_array()){
    
    
    		return $row;
    	}
    
    	return array();
    }     
	// --------------------------------------------------------------------

    /**
	 * 创建
	 *
	 *
	 */	
    function create()
    { 
		 
    	$this->db->set('shipping_no', $this->shipping_no);         
		$this->db->set('shipper', $this->shipper);
		$this->db->set('shipper_telephone', $this->shipper_telephone);
		$this->db->set('receiver', $this->receiver);
		$this->db->set('receiver_telephone', $this->receiver_telephone);
		$this->db->set('delivery_methmod', $this->delivery_methmod);
        $this->db->set('payment_mode', $this->payment_mode);
		$this->db->set('goods_name', $this->goods_name);
		$this->db->set('goods_num', $this->goods_num); 
		$this->db->set('goods_packet_num', $this->goods_packet_num);
		$this->db->set('goods_value', $this->goods_value);
		$this->db->set('transfer_station', $this->transfer_station);
		$this->db->set('freight', $this->freight);
		$this->db->set('cod_fee', $this->cod_fee);
		$this->db->set('actual_fee', $this->actual_fee);
		$this->db->set('transfer_fee', $this->transfer_fee);
		$this->db->set('checkin_site', $this->checkin_site);
		$this->db->set('destination_site', $this->destination_site);
		$this->db->set('operator', $this->operator);
		$this->db->set('operate_datetime', $this->operate_datetime);
		$this->db->set('demo', $this->demo);			 
		$this->db->set('v_fee', $this->v_fee);              
        return $this->db->insert('shipping_list');
    }
 
	// --------------------------------------------------------------------

    /**
	 * 结果集
	 *
	 *
	 */	
    function find_shipping_lists($options = array(), $count=20, $offset=0)
	{
		if (!is_array($options)){
            return array();
        }

        if ($count){
            $this->db->limit((int)$count, (int)$offset);
        }
        
		//shipping_list
        $this->db->select('*');
		 
        

        $query = $this->_query_shipping_lists($options);
        
        $rows = array();
        foreach ($query->result_array() as $row){
            $rows[] = $row;
        }
        return $rows;
	}
	/*
	 * 报表结果
	 */
	function find_shipping_lists_report($st,$et,$report_type,$destination_site)
	{
		$sql  ="select * from v_shipping_list";
		
		if ($report_type == '1')
		{
			$sql  = "SELECT  * FROM v_shipping_list  WHERE   operate_datetime BETWEEN  ? AND ?  ";
		}elseif ($report_type =='2' ) 
    	{
    	    $sql  = "SELECT  * FROM v_shipping_list  WHERE   operate_datetime BETWEEN  ? AND ?   AND (cod_fee >0) ";
    	
    	} 
     
    	
    	if ( $destination_site  !=  '0' )
    	{
    		 $sql .= ' AND destination_site = ? ';
    	} 
     
		$query = $this->db->query($sql,array($st,$et,$destination_site));
    
    	$rows = array();
    	$sum_actual_fee =0;
    	$sum_cod_fee =0;
    	$sum_freight = 0;
    	$sum_transfer_fee =0;
    	
    	foreach ($query->result_array() as $row){
    		$rows[] = $row;

    	}

    	return $rows;

	}    
	
	/**
	 * 
	 * 计算各种付款方式合计
	 */
    // --------------------------------------------------------------------

	function find_shipping_lists_report_paymentmode($st,$et,$report_type,$destination_site)
	{
		$sql  ="select * from v_shipping_list";
	
		if ($report_type == '1')
		{
			$sql  = "SELECT  payment_mode_name ,sum(freight) as sum_fee FROM v_shipping_list  WHERE   operate_datetime BETWEEN  ? AND ?  ";
		}elseif ($report_type =='2' )
		{
			$sql  = "SELECT  * FROM v_shipping_list  WHERE   operate_datetime BETWEEN  ? AND ?   AND (cod_fee >0) ";
			 
		}
		 
		 
		if ( $destination_site  !=  '0' )
		{
			$sql .= ' AND destination_site = ? ';
		}
		 
		$sql .= ' GROUP BY payment_mode_name';
		
		
		$query = $this->db->query($sql,array($st,$et,$destination_site));
	
		$rows = array();
		$sum_actual_fee =0;
		$sum_cod_fee =0;
		$sum_freight = 0;
		$sum_transfer_fee =0;
		 
		foreach ($query->result_array() as $row){
			$rows[] = $row;
	
		}
	
		return $rows;
	
	}
		
	
	
    /**
	 * 更新
	 *
	 *
	 */	
    function  update($id)
    {
         
    	$this->db->set('shipper', $this->shipper);
    	$this->db->set('shipper_telephone', $this->shipper_telephone);
    	$this->db->set('receiver', $this->receiver);
    	$this->db->set('receiver_telephone', $this->receiver_telephone);
    	$this->db->set('delivery_methmod', $this->delivery_methmod);
    	$this->db->set('payment_mode', $this->payment_mode);
    	$this->db->set('goods_name', $this->goods_name);
    	$this->db->set('goods_num', $this->goods_num);
    	$this->db->set('goods_packet_num', $this->goods_packet_num);
    	$this->db->set('goods_value', $this->goods_value);
    	$this->db->set('transfer_station', $this->transfer_station);
    	$this->db->set('freight', $this->freight);
    	$this->db->set('cod_fee', $this->cod_fee);
    	$this->db->set('actual_fee', $this->actual_fee);
    	$this->db->set('transfer_fee', $this->transfer_fee);
    	$this->db->set('checkin_site', $this->checkin_site);
    	$this->db->set('destination_site', $this->destination_site);
    	$this->db->set('operator', $this->operator);
    	$this->db->set('operate_datetime', $this->operate_datetime);
    	$this->db->set('demo', $this->demo);  
    	$this->db->set('v_fee', $this->v_fee);
        $this->db->where('id', $id);
        return $this->db->update('shipping_list');
    }
    	 
    // --------------------------------------------------------------------

    /**
	 * 删除
	 *
	 *
	 */	
	 
    function delete($id)
    {
 
		$this->db->where('id', $id);
        return $this->db->delete('shipping_list'); 
    }

    // --------------------------------------------------------------------

 
 

    /**
	 * 私有函数
	 *
	 *
	 */
    function _query_shipping_lists($options = null)
    {
        $this->db->from('v_shipping_list');
      
       //print_r($options); 

		if (!empty($options['conditions'])){
			foreach($options['conditions'] as $key => $value ):
				switch($key){
                    
    			case 'shipper' : $this->db->like($key,$value);break;
    			case 'shipper_telephone': $this->db->like($key,$value);break;
    			case 'receiver': $this->db->like($key,$value);break;
    			case 'receiver_telephone': $this->db->like($key,$value);break;
    			case 'shipper_no': if (  !empty($value)  ) $this->db->like($key,$value);break;
    			case 'checkin_site': if ($value != '0' ) $this->db->like($key,$value);break;
    			case 'destination_site': if ( $value != '0' ) $this->db->like($key,$value);break;    			
    			case 'operator_name': $this->db->like($key,$value);break;
    			case 'st': $this->db->where('operate_datetime >=',$value);break;
    			case 'et': $this->db->where('operate_datetime <=',$value);break;
					default : $this->db->where($key,$value);
			    }
			    

			endforeach;
        }
        
        if (isset($options['order'])){
            $this->db->order_by($options['order']);
        } else {
            $this->db->order_by('id DESC');
        }
        

        return $this->db->get();
    }

	// --------------------------------------------------------------------

    /**
	 * 总数
	 *
	 *
	 */	
	function count_shipping_lists($options = array())
    {
        $this->db->select('COUNT(DISTINCT(id)) as total');
        
        $query = $this->_query_shipping_lists($options);
        
        $total = 0;
        if ($row = $query->row_array()){
            $total = (int)$row['total'];
        }
        return $total;
    }
  



	// --------------------------------------------------------------------

    /**
	 * 更新单个字段
	 *
	 *
	 */	
	function update_one($fields=array(),$id)
    {      
		$this->db->set(key($fields),current($fields));    
        $this->db->where('id', $id);
        $this->db->update('shipping_list');

		$this->db->select(key($fields));
        $this->db->where('id', $id);
		$query = $this->db->get('shipping_list');      
        $field =  $query->row_array();
		return $field[key($fields)];

    }
    
    
    
    /**
     * 读取所有记录到数组
     */
    function load_all($condition){
    	if (! is_array($condition))
    		return array();
    	 $this->db->order_by('operate_datetime','asc');
    	 
         if (sizeof($condition) > 0) 
         	$query = $this->db->get_where('shipping_list',$condition);
         else 
         	$query = $this->db->get('shipping_list');         	
 
        $rows = array();
        foreach ($query->result_array() as $row){
            $rows[] = $row;
        }
        $this->firephp->log($this->db->last_query());
        return $rows;

        return array();   	
    }
    function get_all()
    {
    	$query = $this->db->get('shipping_list');
    	$rows = array();
    	foreach ($query->result_array() as $row){
    		$rows[$row['id']] = $row;
    	}
    	return $rows;
    }
    
    /*
     * 
     * 根据日期查询订单
     */
    function search($options =null)
    {
    	$sql  = "SELECT * FROM v_shipping_list";
  
    	
    	if (!empty($options['conditions'])){
    		foreach($options['conditions'] as $key => $value ):
    		switch($key){
    	
    			case 'shipper' : $this->db->like($key,$value);break;
    			case 'shipper_telephone': $this->db->like($key,$value);break;
    			case 'receiver': $this->db->like($key,$value);break;
    			case 'receiver': $this->db->like($key,$value);break;
    			case 'shipper_no': $this->db->like($key,$value);break;
    			case 'checkin_site': $this->db->like($key,$value);break;
    			case 'destination_site': $this->db->like($key,$value);break;    			
    			case 'operator_name': $this->db->like($key,$value);break;
    			case 'st': $this->db->where('operate_datetime >=',$value);break;
    			case 'et': $this->db->where('operate_datetime <=',$value);break;
    			default : $this->db->where($key,$value);
    		}
    		endforeach;
    	}
    	
    	if (isset($options['order'])){
    		$this->db->order_by($options['order']);
    	} else {
    		$this->db->order_by('id DESC');
    	}
    	
    	return $this->db->get();  
    }
    /*
     * 根据ID返回发货站点
     * 
     */
    function get_checkin_site($id){
    	if (!$id){
    		return array();
    	}
    	
    	$query = $this->db->get_where('shipping_list',array('id' => $id));
    	
    	if ($row = $query->row_array()){
    	
    	
    		return $row['checkin_site'];
    	}
    	
    	return 0;
    }     
 
}